package com.iqiyi.pps.epg.core.dao.record;

import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.record.TRecordChannel;
import com.iqiyi.pps.epg.api.model.web.record.TRecordChannelSimple;
import com.iqiyi.pps.epg.api.model.web.record.TRecordChannelSimplePage;
import com.iqiyi.pps.epg.core.dao.base.BaseDao;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.model.record.RecordChannel;
import com.iqiyi.pps.epg.core.model.record.RecordPlan;
import com.iqiyi.pps.epg.core.utils.TransUtil;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by karkarrotxu on 2018/4/11.
 */
@Component
public class RecordChannelDao extends BaseDao<RecordChannel> {

    public TRecordChannelSimplePage findChannelSimplePage(TPage page, TFilter filter) {
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);

        Page<RecordChannel> dbPage = TransUtil.buildPage(page);
        Page<RecordChannel> pageData = findPage(dbPage, filterList);
        List<RecordChannel> dataList = pageData.getResult();
        TRecordChannelSimplePage dataPage = new TRecordChannelSimplePage();
        page.setTotalCount(pageData.getTotalCount());
        dataPage.setTPage(page);
        List<TRecordChannelSimple> tList = transToTRecordChannelSimpleList(dataList);
        dataPage.setResult(tList);
        return dataPage;
    }

    private List<TRecordChannelSimple> transToTRecordChannelSimpleList(List<RecordChannel> dataList) {
        List<TRecordChannelSimple> resultList = new ArrayList<>();
        TRecordChannelSimple tPlan = null;
        for (RecordChannel tempData : dataList) {
            tPlan = transToTRecordChannelSimple(tempData);
            resultList.add(tPlan);
        }
        return resultList;
    }

    public TRecordChannelSimple transToTRecordChannelSimple(RecordChannel tempData) {
        TRecordChannelSimple _tEntity = new TRecordChannelSimple();
        if (tempData == null) {
            return _tEntity;
        }
        _tEntity.setId(tempData.getId());
        _tEntity.setChannelCode(tempData.getChannelCode());
        _tEntity.setChannelName(tempData.getChannelName());
        _tEntity.setStatus(tempData.getStatus());
        return _tEntity;
    }

    public Page<RecordChannel> findChannelPage(TPage page, TFilter filter) {
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        Page<RecordChannel> dbPage = TransUtil.buildPage(page);
        Page<RecordChannel> pageData = findPage(dbPage, filterList);
        return pageData;
    }

    public List<TRecordChannel> transToTRecordChannelList(List<RecordChannel> dataList) {
        List<TRecordChannel> resultList = new ArrayList<>();
        TRecordChannel tPlan = null;
        for (RecordChannel tempData : dataList) {
            tPlan = transToTRecordChannel(tempData);
            resultList.add(tPlan);
        }
        return resultList;
    }

    public TRecordChannel transToTRecordChannel(RecordChannel tempData) {
        TRecordChannel _tEntity = new TRecordChannel();
        if (tempData == null) {
            return _tEntity;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        _tEntity.setId(tempData.getId());
        _tEntity.setChannelCode(tempData.getChannelCode());
        _tEntity.setChannelName(tempData.getChannelName());
        _tEntity.setStatus(tempData.getStatus());
        _tEntity.setHasCaption(tempData.isHasCaption() ? 1 : 0);
        _tEntity.setUpdateTime(sdf.format(new Date(tempData.getUpdateTime().getTime())));
        _tEntity.setOpUser(tempData.getOpUser());
        _tEntity.setCreateTime(sdf.format(new Date(tempData.getCreateTime().getTime())));
        return _tEntity;
    }

    public void delRecordChannel(long channelId) {
        RecordChannel channel = get(channelId);
        channel.setStatus(0);
        save(channel);
    }


}
